<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="50%">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="openid" prop="openid">
        <el-input :readonly="formType == 'detail'" v-model="formData.openid" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="是否关注" prop="subscribe">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.subscribe"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option key="1" label="是" value="1" />
          <el-option key="0" label="否" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="关注时间" prop="subscribeTime">
        <el-date-picker
          :readonly="formType == 'detail'"
          v-model="formData.subscribeTime"
          type="date"
          value-format="x"
          placeholder="选择"
        />
      </el-form-item>
      <el-form-item label="语言" prop="language">
        <el-input :readonly="formType == 'detail'" v-model="formData.language" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="unionid" prop="unionid">
        <el-input :readonly="formType == 'detail'" v-model="formData.unionid" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input :readonly="formType == 'detail'" v-model="formData.remark" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="分组ID" prop="groupid">
        <el-input :readonly="formType == 'detail'" v-model="formData.groupid" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="标签ID" prop="tagidList">
        <el-input :readonly="formType == 'detail'" v-model="formData.tagidList" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="渠道来源" prop="subscribeScene">
        <el-select
          :disabled="formType == 'detail'"
          v-model="formData.subscribeScene"
          clearable
          filterable
          class="!w-240px"
        >
          <el-option label="公众号搜索" value="ADD_SCENE_SEARCH" />
          <el-option label="公众号迁移" value="ADD_SCENE_ACCOUNT_MIGRATION" />
          <el-option label="名片分享" value="ADD_SCENE_PROFILE_CARD" />
          <el-option label="扫描二维码" value="ADD_SCENE_QR_CODE" />
          <el-option label="图文页内名称点击" value="ADD_SCENE_PROFILE_LINK" />
          <el-option label="图文页右上角菜单" value="ADD_SCENE_PROFILE_ITEM" />
          <el-option label="支付后关注" value="ADD_SCENE_PAID" />
          <el-option label="微信广告" value="ADD_SCENE_WECHAT_ADVERTISEMENT" />
          <el-option label="他人转载" value="ADD_SCENE_REPRINT" />
          <el-option label="视频号直播" value="ADD_SCENE_LIVESTREAM" />
          <el-option label="视频号" value="ADD_SCENE_CHANNELS" />
          <el-option label="小程序关注" value="ADD_SCENE_WXA" />
          <el-option label="其他" value="ADD_SCENE_OTHERS" />
        </el-select>
      </el-form-item>
      <el-form-item label="二维码扫码场景" prop="qrScene">
        <el-input :readonly="formType == 'detail'" v-model="formData.qrScene" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="二维码扫码场景描述" prop="qrSceneStr">
        <el-input :readonly="formType == 'detail'" v-model="formData.qrSceneStr" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button v-if="formType != 'detail'" @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { WeixinUserApi, WeixinUserVO } from '@/api/integration/weixinuser'
import { dateFormatter, dateFormatter3 } from '@/utils/formatTime'

/** 微信公众号用户基本信息 表单 */
defineOptions({ name: 'WeixinUserForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  subscribe: undefined,
  openid: undefined,
  language: undefined,
  subscribeTime: undefined,
  unionid: undefined,
  remark: undefined,
  groupid: undefined,
  tagidList: undefined,
  subscribeScene: undefined,
  qrScene: undefined,
  qrSceneStr: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await WeixinUserApi.getWeixinUser(id)
      formData.value.subscribe = formData.value.subscribe + ''
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as WeixinUserVO
    if (formType.value === 'create') {
      await WeixinUserApi.createWeixinUser(data)
      message.success(t('common.createSuccess'))
    } else {
      await WeixinUserApi.updateWeixinUser(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    subscribe: undefined,
    openid: undefined,
    language: undefined,
    subscribeTime: undefined,
    unionid: undefined,
    remark: undefined,
    groupid: undefined,
    tagidList: undefined,
    subscribeScene: undefined,
    qrScene: undefined,
    qrSceneStr: undefined
  }
  formRef.value?.resetFields()
}
</script>
